import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
import babel from '@rollup/plugin-babel';

const path = require('path');
const postcss = require('rollup-plugin-postcss');
console.log('00', path.resolve(__dirname, 'babel.config.js'))
const plugins = [
  typescript({
    tsconfig: 'tsconfig.json',
  }),
  babel({
    babelHelpers: 'bundled',
    extensions: ['.js', '.jsx', '.tsx', 'ts'],
    exclude: 'node_modules/**',
    configFile: path.resolve(__dirname, 'babel.config.js'),
    babelHelpers: 'bundled'
  }),
  postcss({
    modules: true, // 增加 css-module 功能
    extensions: ['.less', '.css'],
    use: [
      ['less', {
        javascriptEnabled: true
      }]
    ],
    inject: true,
    extract: false,
  }),
];
const stylePlugins = [
  postcss({
    plugins: [cssnano()],
    // extract: 'bundle.css' // 输出路径
  }),
]


export default [
  {
    input: 'src/chrome-plugin/content-script.ts',
    external: [
      'moment',
      'jquery'
    ],
    output: [
      {
        file: 'chrome-plugin/dist/content-script.js', format: 'iife',
        globals: {
          'moment': 'moment',
          'jquery': '$'
        },
        name: 'option', sourcemap: true
      },
    ],
    plugins,
  },
  {
    input: 'src/chrome-plugin/background.ts',
    output: [
      { file: 'chrome-plugin/dist/background.js', format: 'iife', name: 'option', sourcemap: true },
    ],
    plugins,
  },
  // 生成注入页面的css 问题
  {
    input: 'src/style/index.ts',
    plugins: stylePlugins,
    output: {
      file: 'chrome-plugin/dist/style.js'
    }
  }
];